﻿using System;
using SharpKit.JavaScript;

namespace SharpKit.Onx
{
    /// <summary>
    /// This component enables registering on various application-related events. In addition it enables querying for application data.
    /// https://www.onx.ms/#apiPage/applications
    /// </summary>
    [JsType(Mode = JsMode.Prototype, Export = false)]
    public class Applications : Emitter
    {
        //No way to new this up
        private Applications()
        { }

        #region Actions (Methods)
        /// <summary>
        /// Launches an application with the specified name. If there is more than one application with the same name, the first one will be launched.
        /// </summary>
        /// <param name="appName">the application name (case insensitive)</param>
        [JsMethod(Name = "launch", NativeOverloads = true)]
        public void Launch(string appName)
        { }

        /// <summary>
        /// Launches an application with the specified name. If there is more than one application with the same name, the first one will be launched.
        /// </summary>
        /// <param name="appName">the application name (case insensitive)</param>
        /// <param name="params">the parameters would be passed to the underline Android activity intent</param>
        [JsMethod(Name = "launch", NativeOverloads = true)]
        public void Launch(string appName, object @params)
        { }

        /// <summary>
        /// Launches an application with the specified name. If there is more than one application with the same name, the first one will be launched.
        /// </summary>
        /// <param name="appName">the application name (case insensitive)</param>
        /// <param name="params">the parameters would be passed to the underline Android activity intent</param>
        /// <param name="callback">the callback that will be called upon completion.  Callback signature: function(err); // err contains the error message in case error has occurred or undefined otherwise.</param>
        [JsMethod(Name = "launch", NativeOverloads = true)]
        public void Launch(string appName, object @params, Action<string> callback)
        { }

        /// <summary>
        /// Launches an application by a package name.
        /// </summary>
        /// <param name="packageName">the application package name</param>
        /// <param name="params">the parameters would be passed to the underline Android activity intent</param>
        /// <param name="callback">the callback that will be called upon completion. </param>
        [JsMethod(Name = "launchPackage", NativeOverloads = true)]
        public void LaunchPackage(string packageName, JsObject<string> @params, Action<string> callback)
        { }

        /// <summary>
        /// Launches an application by a package name.
        /// </summary>
        /// <param name="packageName">the application package name</param>
        /// <param name="params">the parameters would be passed to the underline Android activity intent</param>
        [JsMethod(Name = "launchPackage", NativeOverloads = true)]
        public void LaunchPackage(string packageName, JsObject<string> @params)
        { }

        /// <summary>
        /// Launches an application by a package name.
        /// </summary>
        /// <param name="packageName">the application package name</param>
        [JsMethod(Name = "launchPackage", NativeOverloads = true)]
        public void LaunchPackage(string packageName)
        { }


        /// <summary>
        /// Launch the default viewer for the given URL.
        /// </summary>
        /// <param name="url">the URL</param>
        /// <param name="mimeType">the mime type of the URL (e.g 'audio/*')</param>
        [JsMethod(Name = "launchViewer", NativeOverloads = true)]
        public void LaunchViewer(string url, string mimeType)
        { }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the installed apps
        /// </summary>
        [JsProperty(Name = "installedApps", NativeField = true)]
        public InstalledApplication[] InstalledApps { get { return null; } }

        /// <summary>
        /// Gets the running apps
        /// </summary>
        [JsProperty(Name = "runningApps", NativeField = true)]
        public InstalledApplication[] RunningApps { get { return null; } }
        #endregion

        #region Triggers (Events)

        /// <summary>
        /// trigger is received when an application is installed. Additional trigger data is available in the ApplicationSignal parameter passed to the callback function.
        /// </summary>
        public event Action<ApplicationSignal> Installed
        {
            [JsMethod(Name="on",InsertArg0="'installed'")]
            add
            {
            }
            [JsMethod(Name = "removeListener", InsertArg0 = "'installed'")]
            remove
            { 
            }
        }

        /// <summary>
        /// trigger is received when an application is removed. Additional trigger data is available in the ApplicationSignal parameter passed to the callback function.
        /// </summary>
        public event Action<ApplicationSignal> Removed
        {
            [JsMethod(Name = "on", InsertArg0 = "'removed'")]
            add
            {
            }
            [JsMethod(Name = "removeListener", InsertArg0 = "'removed'")]
            remove
            {
            }
        }

        /// <summary>
        /// trigger is received when an application is updated. Additional trigger data is available in the ApplicationSignal parameter passed to the callback function.
        /// </summary>
        public event Action<ApplicationSignal> Updated
        {
            [JsMethod(Name = "on", InsertArg0 = "'updated'")]
            add
            {
            }
            [JsMethod(Name = "removeListener", InsertArg0 = "'updated'")]
            remove
            {
            }
        }
        #endregion
    }

    /// <summary>
    /// application data object which contain: 'name': Application name 'package': Application package name
    /// </summary>
    [JsType(Mode = JsMode.Json, Export = false)]
    public class InstalledApplication
    {
        //No way to new this up
        private InstalledApplication()
        { }

        /// <summary>
        /// Application name
        /// </summary>
        [JsProperty(Name = "name", NativeField = true)]
        public string Name { get; set; }

        /// <summary>
        /// Application package name
        /// </summary>
        [JsProperty(Name = "package", NativeField = true)]
        public string Package { get; set; }
    }

    /// <summary>
    /// This class represents an Application signal. This kind of signal is triggered when a user installs, updates or removes an application.
    /// </summary>
    [JsType(Mode = JsMode.Json, Export = false)]
    public class ApplicationSignal : ScriptSignal
    { 
        /// <summary>
        /// Application name
        /// </summary>
        [JsProperty(Name = "name", NativeField = true)]
        public string Name { get; set; }

        /// <summary>
        /// Application package name
        /// </summary>
        [JsProperty(Name = "package", NativeField = true)]
        public string Package { get; set; }
    
    }
}